package testload;

import cn.hutool.core.io.IoUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import top.lingkang.mm.MagicConfiguration;
import top.lingkang.mm.orm.MapperManage;
import top.lingkang.mm.orm.MapperManageImpl;
import top.lingkang.mmt.entity.UserEntity;
import top.lingkang.mmt.mapper.UserMapper;

import java.io.InputStream;
import java.util.List;

/**
 * @author lingkang
 * Created by 2024/5/3
 */
@Slf4j
public class TestLoad01 {
    public static void main(String[] args) throws Exception {
        Thread.sleep(6000);
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.h2.Driver");
        config.setJdbcUrl("jdbc:h2:mem:mybatismagicdb");
        config.setUsername("root");
        config.setPassword("123456");
        config.setMaximumPoolSize(3);
        HikariDataSource dataSource = new HikariDataSource(config);

        MagicConfiguration configuration = new MagicConfiguration(dataSource);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.addMapperXml("mapper");
        configuration.addMapperXml("mapper/**.xml");
        SqlSessionFactory sessionFactory = configuration.build();
        MapperManage mapperManage = new MapperManageImpl(configuration, sessionFactory.openSession());
        InputStream stream = TestLoad01.class.getClassLoader().getResourceAsStream("script/init-h2.sql");
        String sql = IoUtil.readUtf8(stream);
        log.info("开始执行SQL");
        mapperManage.executeSqlScript(sql);
        log.info("SQL执行完毕");
        try {
            UserMapper userMapper = mapperManage.getMapper(UserMapper.class);
            List<UserEntity> all = userMapper.testAll();
            log.info("testAll {}", all);
            all = userMapper.all();
            log.info("all {}", all);
        } catch (Exception e) {
            log.error("getMapper", e);
        }


    }
}
